package main

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func inDistance(p1, p2 []int) bool {
	r := p1[2]
	x := abs(p1[0] - p2[0])
	y := abs(p1[1] - p2[1])
	return x*x+y*y <= r*r
}

func maximumDetonation(bombs [][]int) int {
	directMap := make(map[int]map[int]struct{}) // bomb -> 被直接波及的炸弹
	for i, x := range bombs {
		xm := make(map[int]struct{})
		xm[i] = struct{}{}
		for j, y := range bombs {
			if i == j {
				continue
			}
			// 判断 x 能否引爆 y
			if inDistance(x, y) {
				xm[j] = struct{}{}
			}
		}
		directMap[i] = xm
	}
	max := 0
	for i := range bombs {
		queue := []int{i}
		detonate := make(map[int]struct{})
		detonate[i] = struct{}{}
		for len(queue) > 0 {
			head := queue[0]
			queue = queue[1:]
			m := directMap[head]
			for k := range m {
				if _, ok := detonate[k]; ok {
					continue
				}
				queue = append(queue, k)
				detonate[k] = struct{}{}				
			}
		}
		if len(detonate) > max {
			max = len(detonate)
		}
	}
	return max
}

func main() {
	bombs := [][]int{
		{1, 2, 3},
		{2, 3, 1},
		{3, 4, 2},
		{4, 5, 3},
		{5, 6, 4},
	}
	ret := maximumDetonation(bombs)
	println(ret)
}
